package shiro.com.config;


import org.apache.shiro.mgt.DefaultSecurityManager;
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.apache.shiro.spring.web.ShiroFilterFactoryBean;
import org.apache.shiro.mgt.SecurityManager;

import org.apache.shiro.web.mgt.DefaultWebSecurityManager;
import org.mapstruct.Qualifier;
import org.springframework.aop.framework.autoproxy.DefaultAdvisorAutoProxyCreator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import shiro.com.utils.shiro.MyCredentialsMatcher;
import shiro.com.utils.shiro.filter.JwtFilter;
import shiro.com.utils.shiro.realm.MyShiroRealm;

import javax.servlet.Filter;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * Created by yangqj on 2017/4/23.
 */
@Configuration
public class ShiroConfig {
    @Autowired
    private MyShiroRealm myShiroRealm;
    @Autowired
    private MyCredentialsMatcher myCredentialsMatcher;


    @Bean
    public DefaultSecurityManager getDefaultSecurityManager() {
        DefaultWebSecurityManager defaultSecurityManager = new DefaultWebSecurityManager();
        myShiroRealm.setCredentialsMatcher(myCredentialsMatcher);
        defaultSecurityManager.setRealm(myShiroRealm);
        return defaultSecurityManager;
    }

    @Bean
    public ShiroFilterFactoryBean getShiroFilterFactoryBean(DefaultSecurityManager defaultSecurityManager,JwtFilter jwtFilter) {
        ShiroFilterFactoryBean shiroFilterFactoryBean = new ShiroFilterFactoryBean();
        shiroFilterFactoryBean.setSecurityManager(defaultSecurityManager);
        Map<String, Filter> filterMap = new HashMap<String, Filter>();
        filterMap.put("jwtFlter",jwtFilter);
        shiroFilterFactoryBean.setFilters(filterMap);
        //过滤
        LinkedHashMap<String, String> map = new LinkedHashMap<>();

        map.put("/swagger-ui.html**", "anon");
        map.put("/v2/api-docs", "anon");
        map.put("/swagger-resources/**", "anon");
        map.put("/webjars/**", "anon");
//
//       map.put("/system/comont/tree","anon"); //不需要登录
//       map.put("/system/resoure/login2","authc"); //需要登录
//       map.put("/system/xy/*","anon"); //不需要登录
        map.put("/","jwtFlter");
        shiroFilterFactoryBean.setFilterChainDefinitionMap(map);
        shiroFilterFactoryBean.setLoginUrl("/");
        return shiroFilterFactoryBean;
    }
    @Bean
    public JwtFilter getJwtFilter(){
        return    new JwtFilter();
    }

    // 开启注解代理

    // 开启注解代理
    @Bean
    public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor(SecurityManager securityManager) {
        AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor = new AuthorizationAttributeSourceAdvisor();
        authorizationAttributeSourceAdvisor.setSecurityManager(securityManager);
        return authorizationAttributeSourceAdvisor;
    }

    @Bean
    public DefaultAdvisorAutoProxyCreator defaultAdvisorAutoProxyCreator() {
        DefaultAdvisorAutoProxyCreator creator = new DefaultAdvisorAutoProxyCreator();
        creator.setProxyTargetClass(true);
        return creator;
    }
}
